package com.loan.mappers;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.loan.entity.Role;
import com.loan.util.AUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;

import java.lang.reflect.Type;
import java.util.List;

/**
 * YJS
 */
public interface RoleMapper extends BaseMapper<Role> {

    /**
     * 添加角色
     * @param role
     */
    @Insert("insert into role(name,description,status) values(#{name},#{description},#{status})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int addRole(Role role);

    /**
     * 查询所有角色的权限
     * @return
     */
    @Select("select id,name,description,status from role")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "description" , column = "description"),
            @Result(property = "status",column = "status"),
            @Result(property = "perms",column = "id" , many = @Many(select = "com.loan.mappers.PermMapper.getByRoleId",fetchType = FetchType.LAZY))
    })
    List<Role> getRoleAll();

    /**
     * 查询所有角色
     * @return
     */
    @Select("select * from role where status='y'")
    List<Role> getRoleall();

    @Select("select * from role where id=#{roleid}")
    Role getByRoleId(int roelid);


    /**
     * 根据名字查询角色
     * @param name
     * @return
     */
    @Select("select * from role where name=#{name}")
    Role getByRoleName(String name);

    /**
     * 根据id修改角色状态表
     * @param id
     */
    @Update("UPDATE role SET status='n' where id=#{id}")
    void deleteRoleId(int id);


    /**
     * 模糊查询
     * @param role
     * @return
     */
    @SelectProvider(type = SqlProvider.class,method = "select")
    List<Role> selectByRole(Role role);


    /**
     * 动态sql，根据id修改角色信息
     * @param role
     */
    @UpdateProvider(type = SqlProvider.class,method = "update")
    void updateRole(Role role);
    public class SqlProvider{
            public String update(Role role){

                SQL sql = new SQL();
                sql.UPDATE("role");
                if(AUtils.isNotEmpty(role.getName())){
                    sql.SET("name=#{name}");
                }
                if(AUtils.isNotEmpty(role.getDescription())){
                    sql.SET("description=#{description}");
                }
                if(AUtils.isNotEmpty(role.getStatus())){
                    sql.SET("status=#{status}");
                }
                sql.WHERE("id=#{id}");
                return sql.toString();
            }

            public String select(Role role){
                SQL sql = new SQL();
                sql.SELECT("role");
                if(role.getId() != null ){
                    sql.WHERE("id=#{id}");
                }
                if(role.getName() != null){
                    sql.WHERE("name=#{name}");
                }
                if(role.getDescription() != null){
                    sql.WHERE("description=#{description}");
                }
                return sql.toString();
            }
    }
}
